package com.sxpi.common.result;

import cn.hutool.core.bean.BeanUtil;
import com.sxpi.common.enums.AdminResultInterface;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 董伟豪
 * @Date: 2022/10/3 16:25
 */
public class R extends HashMap<String, Object> {

    private R() {
        put("code", 200);
        put("msg", "success");
    }

    public static R ok() {
        return new R();
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    /**
     * map合并
     */
    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    /**
     * 对象转Map
     * @param object
     * @return
     */
    public static R ok(Object object) {
        R r = new R();
        // 快速的把一个对象转换成map
        r.putAll(BeanUtil.beanToMap(object));
        return r;
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R error(AdminResultInterface adminResultInterface) {
        R r = new R();
        r.put("code", adminResultInterface.getCode());
        r.put("msg", adminResultInterface.getMsg());
        return r;
    }

    public static R error(String msg) {
        return error(501, msg);
    }

    public static R error() {
        return error(500, "未知异常，请联系管理员");
    }


    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
